#include "GPS_IPC.h"
//Parse GPS data
//
// Created by ubuntu on 6/10/23.
//
/*
 * @param:string
 * GPS data that needs to be parsed
 * @return
 * GPGGA structure
 * */
gpgga parse_strings(char *string) {
    gpgga gps_data;
    char *token;
    token = strtok(string, ",");
    int n = 0;
    while (token != NULL) {
        switch (n) {
            case 0:
                strcpy(gps_data.gga, token);
                break;
            case 1:
                strcpy(gps_data.time, token);
                break;
            case 2:
                strcpy(gps_data.latitude, token);
                break;
            case 3:
                strcpy(gps_data.NS, token);
                break;
            case 4:
                strcpy(gps_data.longitude, token);
                break;
            case 5:
                strcpy(gps_data.EW, token);
                break;
            case 6:
                strcpy(gps_data.gps_status, token);
                break;
            case 7:
                strcpy(gps_data.wx_num, token);
                break;
            case 8:
                strcpy(gps_data.HDOP, token);
                break;
            case 9:
                strcpy(gps_data.HBGD, token);
                break;
            case 10:
                strcpy(gps_data.height, token);
                if (strcmp(gps_data.height, "M") == 0)n--;
                break;
            case 11:
                strcpy(gps_data.cf_time, token);
                if (strcmp(gps_data.cf_time, "M") == 0)n--;
                break;
            case 12:
                strcpy(gps_data.cf_ID, token);
                if (strcmp(gps_data.cf_ID, "M") == 0)n--;
                break;
        }
        token = strtok(NULL, ",");//strtok第一个参数传入NULL，使用之前保存的SAVE_PTR定位下一个待处理的字符的位置
        n++;
    }
    return gps_data;
}

char *join(char *a, char *b) {
    char *c = (char *) malloc(strlen(a) + strlen(b) + 1);
    //局部变量，用malloc申请内存,strlen不算'\0'，所以需要+1
    if (c == NULL) exit(1);
    char *tempc = c; //把首地址存下来
    while (*a != '\0') {
        *c++ = *a++;
    }
    while ((*c++ = *b++) != '\0') { ;
    }
    //注意，此时指针c已经指向拼接之后的字符串的结尾'\0' !
    return tempc;
    //返回值是局部malloc申请的指针变量，需在函数调用结束后free。
}

char *substring(char *dst, char *src, int start, int len) {
    char *p = dst;
    char *q = src;
    int length = strlen(src);
    if (start >= length || start < 0)
        return NULL;
    if (len > length)
        len = length - start;
    q += start;
    while (len--) {
        *(p++) = *(q++);
    }
    *(p++) = '\0';
    return dst;
}

char *toString(gpgga data) {
    char *step0 = join("符号:", data.gga);
    char *step1 = join("UTC 时间:", data.time);
    char *step2 = join("\n纬度:", data.latitude);
    char *step3 = join("\n南北半球:", data.NS);
    char *step4 = join("\n经度:", data.longitude);
    char *step5 = join("\n东西半球:", data.EW);
    char *step6 = join("\nGPS状态:", data.gps_status);
    char *step7 = join("\n正在使用的卫星数量:", data.wx_num);
    char *step8 = join("\nHDOP水平经度因子:", data.HDOP);
    char *step9 = join("\n海拔高度:", data.HBGD);
    char *step10 = join("\n地球椭球面相对大地水准面的高度:", data.height);
    char *step11 = join("\n保存时间:", data.cf_time);
    char *step12 = join("\n差分站ID:", data.cf_ID);
    return join(
            join(
                    join(
                            join(
                                    join(
                                            join(
                                                    join(
                                                            join(
                                                                    join(
                                                                            join(
                                                                                    join(
                                                                                            join(
                                                                                                    step1, step2),
                                                                                            step3),
                                                                                    step4), step5), step6), step7),
                                                    step8),
                                            step9), step10), step11), step12), "\n");
}

char *string_pretreatment(char *string) {
    int d_index[14] = {0};
    int n = 0;
    char *result;
    for (int i = 0; i < strlen(string); i++) {
        if (string[i] == ',') {
            d_index[n] = i;
            n++;
        }
    }
    for (int i = 1; i < 14; i++) {
        if (d_index[i] - d_index[i - 1] == 1) {
            char temp2[512];
            char temp1[512];
            substring(temp2, string, d_index[i], strlen(string) - d_index[i]);
            string[d_index[i]] = ' ';
            substring(temp1, string, 0, d_index[i] + 1);
            result = join(temp1, temp2);
        }
    }
    printf("%s",result);
    return result;
}
/*

int main() {
    char raw_string[512] = "$GPGGA,044744.00,3122.4658,N,12025.2791,E,1,10,3.00,12.575,M,7.100,M,,0000*5F";
    char *string = string_pretreatment(raw_string);
    gpgga data = parse_strings(string);
    printf("%s", toString(data));
}
*/
